package com.controller;

import com.bean.dto.Dto;
import com.bean.entity.MainGoods;
import com.service.MainGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("m")
public class MainGoodsController {
    //自动注入
    @Autowired
    MainGoodsService mainGoodsService;
    /* 查询 */
    @RequestMapping("queryAll")
    public Dto queryAll(MainGoods mainGoods){
        //执行查询语句
        List<MainGoods> mainGoodsList = mainGoodsService.queryAll(mainGoods);
        if(mainGoodsList.size() == 0){
            return new Dto("0","为查询到相关信息!",null,"失败!");
        }else{
            return new Dto("200","查询成功!",mainGoodsList,"成功!");
        }
    }
    /* 添加 */
    @RequestMapping("save")
    public Dto save(MainGoods mainGoods){
        //添加信息
        System.out.println(">>> 添加");
        //执行
        try {
            mainGoodsService.save(mainGoods);
            return new Dto("200","添加成功!",null,"成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return new Dto("0","添加失败",null,"失败");
        }
    }
    /* 修改 */
    @RequestMapping("modify")
    public Dto modify(MainGoods mainGoods){
        //修改信息
        System.out.println(">>> 修改");
        //执行
        try {
            mainGoodsService.modify(mainGoods);
            return new Dto("200","修改成功!",null,"成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return new Dto("0","修改成功!",null,"失败!");
        }
    }
    /* 删除 */
    @RequestMapping("remove")
    public Dto remove(MainGoods mainGoods){
        //删除信息
        System.out.println(">>> 删除");
        //执行
        try {
            mainGoodsService.remove(mainGoods);
            return new Dto("200","删除成功!",null,"成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return new Dto("0","删除成功!",null,"失败!");
        }
    }
}
